package springboot.config;

import springboot.annotation.MyConditionalOnClass;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * 自定义条件判断
 */
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        try {
            //获取注解属性Map
            Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(MyConditionalOnClass.class.getName());
            String className = (String) annotationAttributes.get("value");
            //获取类加载器
            ClassLoader classLoader = context.getClassLoader();
            //加载类
            classLoader.loadClass(className);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
